/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package gui.panels;

import model.Lamel;

import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Memet
 */
public class LayerPanel extends javax.swing.JPanel {

    //Materiaal m;
    Lamel m;
    /**
     * Creates new form LayerPanel
     */
    public LayerPanel() {
        initComponents();
        m = null;
        updateForm();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel2 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        tblMaterials = new javax.swing.JTable();
        jScrollPane1 = new javax.swing.JScrollPane();
        tblUDMaterials = new javax.swing.JTable();

        jLabel2.setFont(new java.awt.Font("Trebuchet MS", 1, 18)); // NOI18N
        jLabel2.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel2.setText("Fabric");

        jLabel1.setFont(new java.awt.Font("Trebuchet MS", 1, 18)); // NOI18N
        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel1.setText("UD");

        tblMaterials.setModel(new javax.swing.table.DefaultTableModel(
                new Object [][] {
                        {"fsa", "wew", null, null},
                        {null, null, null, null},
                        {null, null, null, null},
                        {null, null, null, null}
                },
                new String [] {
                        "Title 1", "Title 2", "Title 3", "Title 4"
                }
        ));
        tblMaterials.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        tblMaterials.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tblMaterialsMouseClicked(evt);
            }
        });
        jScrollPane2.setViewportView(tblMaterials);

        tblUDMaterials.setModel(new javax.swing.table.DefaultTableModel(
                new Object [][] {
                        {"fsa", "wew", null, null},
                        {null, null, null, null},
                        {null, null, null, null},
                        {null, null, null, null}
                },
                new String [] {
                        "Title 1", "Title 2", "Title 3", "Title 4"
                }
        ));
        tblUDMaterials.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        tblUDMaterials.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tblUDMaterialsMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(tblUDMaterials);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, 509, Short.MAX_VALUE)
                        .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, 509, Short.MAX_VALUE)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 509, Short.MAX_VALUE)
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 509, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(1, 1, 1)
                                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 132, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, 22, Short.MAX_VALUE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 141, javax.swing.GroupLayout.PREFERRED_SIZE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void tblMaterialsMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tblMaterialsMouseClicked
        if (tblMaterials.getSelectedRow() != -1) {
            tblUDMaterials.clearSelection();
        }
        updateForm();
    }//GEN-LAST:event_tblMaterialsMouseClicked

    private void tblUDMaterialsMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tblUDMaterialsMouseClicked
        if (tblUDMaterials.getSelectedRow() != -1) {
            tblMaterials.clearSelection();
        }
        updateForm();
    }//GEN-LAST:event_tblUDMaterialsMouseClicked


    public void updateForm()
    {
        int selectionUD = tblUDMaterials.getSelectedRow();
        int selection = tblMaterials.getSelectedRow();



        //materialen tabel updaten
        DefaultTableModel model = new DefaultTableModel(
                new Object[][]{},
                new String[]{
                        "Name", "El [GPa]", "Et [GPa]", "vlt", "Glt [GPa]","ρ [Kg/m3]", "Manufacturer", "Plot color"
                }){public boolean isCellEditable(int rowIndex, int colIndex) {
            return false;   //Disallow the editing of any cell
        }};
        tblUDMaterials.setModel(model);
        /* todo refactor
        Iterator<Lamel> it = Global.data.getLamels();

        while(it.hasNext())
        {
            Lamel v = it.next();
            if(v.getType().equals(MaterialDirection.UNIDIRECTIONAAL))
            {
                String rgbf = Integer.toHexString(v.getColorSet().getFiber().getRGB());
                rgbf = rgbf.substring(2, rgbf.length());
                String rgbm = Integer.toHexString(v.getColorSet().getBackground().getRGB());
                rgbm = rgbm.substring(2, rgbm.length());
                model.addRow(new Object[]{v,Calc.Round(v.getEl()*1e-9,2),Calc.Round(v.getEt()*1e-9,2),Calc.Round(v.getMult(),2),Calc.Round(v.getGlt()*1e-9,2),v.getRhol(),v.getManufacturer(), "<html><p bgcolor=\"#"+rgbm+"\"><font color=\"#"+rgbf+"\">--------------------------------------------------------------------------------------------------------------------------------------------------------</font></p></html>"});
            }
        }
        */
        //materialen tabel updaten
        DefaultTableModel model1 = new DefaultTableModel(
                new Object[][]{},
                new String[]{
                        "Name", "E1 [GPa]", "E2 [GPa]", "v12", "G12 [GPa]","ρ [Kg/m3]", "Manufacturer", "Plot color"
                }){public boolean isCellEditable(int rowIndex, int colIndex) {
            return false;   //Disallow the editing of any cell
        }};
        tblMaterials.setModel(model1);
        /* todo refactor
        Iterator<Lamel> it1 = Global.data.getLamels();
        while(it1.hasNext())
        {
            Lamel l = it1.next();
            if(!l.getType().equals(MaterialDirection.UNIDIRECTIONAAL))
            {
                String rgbf = Integer.toHexString(l.getColorSet().getFiber().getRGB());
                rgbf = rgbf.substring(2, rgbf.length());
                String rgbm = Integer.toHexString(l.getColorSet().getBackground().getRGB());
                rgbm = rgbm.substring(2, rgbm.length());
                String example = "<html><p bgcolor=\"#"+rgbm+"\"><font color=\"#"+rgbf+"\">--|--|--|--|--|--|--|--|--|--|--|--|--|--|--|--|--|--|--|--|--|--|--|--|--|--|--|--|--|--|--|--|--|--|--|--|--|--|--|--|--|--|--|--|--|--|--|--|--|--|--</font></p></html>";
                if(l.getType().equals(MaterialDirection.MINVIJFENVEERTIGVIJFENVEERTIG))
                {
                    example = "<html><p bgcolor=\"#"+rgbm+"\"><font color=\"#"+rgbf+"\">xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx</font></p></html>";
                }
                model1.addRow(new Object[]{l,Calc.Round(l.getEl()*1e-9,2),Calc.Round(l.getEt()*1e-9,2),Calc.Round(l.getMult(),2),Calc.Round(l.getGlt()*1e-9,2),l.getRhol(),l.getManufacturer(), example});
            }
        }
        */
        tblUDMaterials.changeSelection(selectionUD, 0, false, false);
        tblMaterials.changeSelection(selection, 0, false, false);

        if(selectionUD==-1)
        {
            if(selection==-1)
            {
                m = null;

            }
            else
            {
                m = (Lamel) tblMaterials.getValueAt(selection, 0);
                LayerOperationPanel pane = new LayerOperationPanel();
                pane.setMateriaal(m);
                //todo refactor
                // Global.menu.setOperations(pane);
            }
        }
        else
        {
            m = (Lamel) tblUDMaterials.getValueAt(selectionUD, 0);
            LayerOperationPanel pane = new LayerOperationPanel();
            pane.setMateriaal(m);
            //todo refactor
            // Global.menu.setOperations(pane);
        }

    }



    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTable tblMaterials;
    private javax.swing.JTable tblUDMaterials;
    // End of variables declaration//GEN-END:variables

    public void clearSelection() {
        tblMaterials.clearSelection();
        tblUDMaterials.clearSelection();
    }
}
